package com.inei.eea2014.controller.activity;

import java.util.ArrayList;
import java.util.List;
import com.inei.eea2014.R;
import com.inei.eea2014.controller.Capitulo1Controller;
import com.inei.eea2014.controller.EntrevistaController;
import com.inei.eea2014.controller.JustificacionController;
import com.inei.eea2014.controller.MarcoController;
import com.inei.eea2014.controller.OpcionesController;
import com.inei.eea2014.model.entity.Capitulo1;
import com.inei.eea2014.model.entity.Ciiu;
import com.inei.eea2014.model.entity.Clanae;
import com.inei.eea2014.model.entity.Dep;
import com.inei.eea2014.model.entity.Dis;
import com.inei.eea2014.model.entity.Entrevista;
import com.inei.eea2014.model.entity.Justificacion;
import com.inei.eea2014.model.entity.Marco;
import com.inei.eea2014.model.entity.Pro;
import com.inei.eea2014.model.entity.TipoOrganizacion;
import com.inei.eea2014.model.entity.TipoVia;
import com.inei.eea2014.tools.Mensaje;
import com.inei.eea2014.tools.VisitasHolder;

import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

public class CapituloI_Fragment extends Fragment {

	// componentes
	private View root;
	private EditText et01, et02, et03, et04, et05, et06, et08, et09, et10,
			et11, et12, et13, et14, et15, et16, et17, et18, et29, et31, et32,
			et33, et34;
	private Spinner sp07, sp19, sp20, sp21, sp22, sp23, sp24;
	private LinearLayout llVisitas;
	private List<View> visitas;

	// variables
	private String RUC;
	private String USUARIO;
	private Capitulo1Controller cap1Con;
	private Capitulo1 obj;
	private MarcoController marCon;
	private OpcionesController opcCon;
	private List<Dep> lDepa;
	private List<Pro> lProv;
	private List<Dis> lDist;
	private List<TipoOrganizacion> lTipOrg;
	private List<Ciiu> lCiiu;
	private List<Clanae> lClan;
	private List<TipoVia> lTipVia;
	private Mensaje m;
	private JustificacionController jusCon;
	private List<Justificacion> lJust;
	private EntrevistaController entCon;
	private List<Entrevista> lEnt;
	private boolean nuevo;
	private boolean nuevoJust = false;
	private String ccdd;
	private String ccpp;
	private int p;
	private int d;
	private int c;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		root = inflater.inflate(R.layout.fragment_capitulo_i, container, false);

		return root;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);

		cap1Con = new Capitulo1Controller(getActivity());
		marCon = new MarcoController(getActivity());
		opcCon = new OpcionesController(getActivity());
		jusCon = new JustificacionController(getActivity());
		entCon = new EntrevistaController(getActivity());
		m = new Mensaje(getActivity());

		d = 0;
		p = 0;
		c = 0;

		IniciarComponentes();

		AgregarEventos();

		CargarDatos();
	}

	private void IniciarComponentes() {
		et01 = (EditText) root.findViewById(R.id.et01);
		et02 = (EditText) root.findViewById(R.id.et02);
		et03 = (EditText) root.findViewById(R.id.et03);
		et04 = (EditText) root.findViewById(R.id.et04);
		et05 = (EditText) root.findViewById(R.id.et05);
		et06 = (EditText) root.findViewById(R.id.et06);
		et08 = (EditText) root.findViewById(R.id.et08);
		et09 = (EditText) root.findViewById(R.id.et09);
		et10 = (EditText) root.findViewById(R.id.et10);
		et11 = (EditText) root.findViewById(R.id.et11);
		et12 = (EditText) root.findViewById(R.id.et12);
		et13 = (EditText) root.findViewById(R.id.et13);
		et14 = (EditText) root.findViewById(R.id.et14);
		et15 = (EditText) root.findViewById(R.id.et15);
		et16 = (EditText) root.findViewById(R.id.et16);
		et17 = (EditText) root.findViewById(R.id.et17);
		et18 = (EditText) root.findViewById(R.id.et18);
		et29 = (EditText) root.findViewById(R.id.et29);
		et31 = (EditText) root.findViewById(R.id.et31);
		et32 = (EditText) root.findViewById(R.id.et32);
		et33 = (EditText) root.findViewById(R.id.et33);
		et34 = (EditText) root.findViewById(R.id.et34);

		sp07 = (Spinner) root.findViewById(R.id.sp07);
		sp19 = (Spinner) root.findViewById(R.id.sp19);
		sp20 = (Spinner) root.findViewById(R.id.sp20);
		sp21 = (Spinner) root.findViewById(R.id.sp21);
		sp22 = (Spinner) root.findViewById(R.id.sp22);
		sp23 = (Spinner) root.findViewById(R.id.sp23);
		sp24 = (Spinner) root.findViewById(R.id.sp24);

		// Spinner TipoVia
		lTipVia = opcCon.TipoViaObtenerTodo();
		TipoVia tipovia = new TipoVia("0", "Seleccionar TipoVia", "Seleccionar");
		lTipVia.add(0, tipovia);
		sp07.setAdapter(new ArrayAdapter<TipoVia>(getActivity(),
				android.R.layout.simple_spinner_item, lTipVia));

		// Spinner Dep
		lDepa = opcCon.DepObtenerTodo();
		Dep dep = new Dep("00", "Seleccionar Departamento");
		lDepa.add(0, dep);
		sp19.setAdapter(new ArrayAdapter<Dep>(getActivity(),
				android.R.layout.simple_spinner_item, lDepa));
		ccdd = "00";
		ccpp = "00";

		// Spinner Ciiu
		lCiiu = opcCon.CiiuObtenerTodo();
		Ciiu ciiu = new Ciiu("0000", "Seleccionar CIIU");
		lCiiu.add(0, ciiu);
		sp22.setAdapter(new ArrayAdapter<Ciiu>(getActivity(),
				android.R.layout.simple_spinner_item, lCiiu));

		// Spinner TipoOrganizacion
		lTipOrg = opcCon.TipoOrganizacionObtenerTodo();
		TipoOrganizacion tipoorganizacion = new TipoOrganizacion("00",
				"Seleccionar Tipo");
		lTipOrg.add(0, tipoorganizacion);
		sp24.setAdapter(new ArrayAdapter<TipoOrganizacion>(getActivity(),
				android.R.layout.simple_spinner_item, lTipOrg));

		llVisitas = (LinearLayout) root.findViewById(R.id.llVisitas);
	}

	private void AgregarEventos() {
		sp19.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> a, View v, int pos,
					long id) {
				Pro pro = new Pro("00", "00", "Seleccionar Provincia");
				if (pos == 0) {
					lProv = new ArrayList<Pro>();
					lProv.add(0, pro);
					sp20.setAdapter(new ArrayAdapter<Pro>(getActivity(),
							android.R.layout.simple_spinner_item, lProv));
				} else {
					ccdd = ((Dep) sp19.getSelectedItem()).getCcdd();
					lProv = opcCon.ProObtenerTodo(ccdd);
					lProv.add(0, pro);
					sp20.setAdapter(new ArrayAdapter<Pro>(getActivity(),
							android.R.layout.simple_spinner_item, lProv));
					if (obj != null && p == 0) {
						for (int i = 0; i < lProv.size(); i++) {
							if (obj.getC0001_020_01_T().equals(
									lProv.get(i).getCcpp())) {
								sp20.setSelection(i);
								break;
							}
						}
						p = 1;
					}
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});

		sp20.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> a, View v, int pos,
					long id) {
				Dis dis = new Dis("00", "00", "00", "Seleccionar Distrito");
				if (pos == 0) {
					lDist = new ArrayList<Dis>();
					lDist.add(0, dis);
					sp21.setAdapter(new ArrayAdapter<Dis>(getActivity(),
							android.R.layout.simple_spinner_item, lDist));
				} else {
					ccpp = ((Pro) sp20.getSelectedItem()).getCcpp();
					lDist = opcCon.DisObtenerTodo(ccdd, ccpp);
					lDist.add(0, dis);
					sp21.setAdapter(new ArrayAdapter<Dis>(getActivity(),
							android.R.layout.simple_spinner_item, lDist));
					if (obj != null && d == 0) {
						for (int i = 0; i < lDist.size(); i++) {
							if (obj.getC0001_021_01_T().equals(
									lDist.get(i).getCcdi())) {
								sp21.setSelection(i);
								break;
							}
						}
						d = 1;
					}
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});

		sp22.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> a, View v, int pos,
					long id) {
				Clanae cla = new Clanae("00000", "Seleccionar Clanae");
				if (pos == 0) {
					lClan = new ArrayList<Clanae>();
					lClan.add(0, cla);
					sp23.setAdapter(new ArrayAdapter<Clanae>(getActivity(),
							android.R.layout.simple_spinner_item, lClan));
				} else {
					lClan = opcCon.ClanaeObtenerTodo(((Ciiu) sp22
							.getSelectedItem()));
					lClan.add(0, cla);
					sp23.setAdapter(new ArrayAdapter<Clanae>(getActivity(),
							android.R.layout.simple_spinner_item, lClan));
					if (obj != null && c == 0) {
						if (obj.getC0001_023_01_T() != null) {
							for (int i = 0; i < lClan.size(); i++) {
								Log.i("CLANAE", obj.getC0001_023_01_T() + "");
								Log.i("CLANAE", lClan.get(i).getCLANAE() + "");
								if (obj.getC0001_023_01_T().equals(
										lClan.get(i).getCLANAE())) {
									sp23.setSelection(i);
									break;
								}
							}
							c = 1;
						}
					}
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});
	}

	private void CargarDatos() {
		RUC = getArguments().getString("RUC");
		USUARIO = getArguments().getString("USUARIO");

		obj = new Capitulo1(RUC, USUARIO);

		obj = cap1Con.Cap01Obtener(obj);
		if (obj == null) {
			nuevo = true;
			Marco marco = new Marco(RUC, USUARIO);

			marco = marCon.MarcoObtener(marco);

			obj = new Capitulo1(RUC, USUARIO);

			if (marco.getRUC() != null) {
				obj.setC0001_001_01_T(marco.getRUC());
			}
			if (marco.getRAZON_SOCIAL() != null) {
				obj.setC0001_002_01_T(marco.getRAZON_SOCIAL());
			}
			if (marco.getTELEFONO() != null) {
				obj.setC0001_004_01_T(marco.getTELEFONO());
			}
			if (marco.getTELEFONO1() != null) {
				obj.setC0001_005_01_T(marco.getTELEFONO1());
			}
			if (marco.getFAX() != 0) {
				obj.setC0001_006_01_T(marco.getFAX() + "");
			}
			if (marco.getDESC_TIPO_VIA() != null) {
				obj.setC0001_007_01_T(marco.getDESC_TIPO_VIA());
			}
			if (marco.getNOMBRE_VIA() != null) {
				obj.setC0001_008_01_T(marco.getNOMBRE_VIA());
			}
			if (marco.getPUERTA() != null) {
				obj.setC0001_009_01_T(marco.getPUERTA());
			}
			if (marco.getINTERIOR() != null) {
				obj.setC0001_010_01_T(marco.getINTERIOR());
			}
			if (marco.getPISO() != null) {
				obj.setC0001_011_01_T(marco.getPISO());
			}
			if (marco.getMANZANA() != null) {
				obj.setC0001_012_01_T(marco.getMANZANA());
			}
			if (marco.getLOTE() != null) {
				obj.setC0001_013_01_T(marco.getLOTE());
			}
			if (marco.getKILOMETRO() != null) {
				obj.setC0001_014_01_T(marco.getKILOMETRO() + "");
			}
			if (marco.getREFERENCIA() != null) {
				obj.setC0001_015_01_T(marco.getREFERENCIA());
			}
			if (marco.getEMAIL() != null) {
				obj.setC0001_017_01_T(marco.getEMAIL());
			}
			if (marco.getWEB() != null) {
				obj.setC0001_018_01_T(marco.getWEB());
			}
			if (marco.getUBIGEO() != null) {
				obj.setC0001_019_01_T(marco.getUBIGEO().substring(0, 2));
			}
			if (marco.getUBIGEO() != null) {
				obj.setC0001_020_01_T(marco.getUBIGEO().substring(2, 4));
			}
			if (marco.getUBIGEO() != null) {
				obj.setC0001_021_01_T(marco.getUBIGEO().substring(4));
			}
			if (marco.getCIIU_V4() != null) {
				obj.setC0001_022_01_T(marco.getCIIU_V4());
			}
			if (marco.getORGANIZACION_JURIDICA() != null) {
				obj.setC0001_024_01_T(marco.getORGANIZACION_JURIDICA()
						.substring(0, 2));
			}
		}

		if (obj != null) {
			et01.setText(obj.getC0001_001_01_T());
			et02.setText(obj.getC0001_002_01_T());
			et03.setText(obj.getC0001_003_01_T());
			et04.setText(obj.getC0001_004_01_T());
			et05.setText(obj.getC0001_005_01_T());
			et06.setText(obj.getC0001_006_01_T());
			if (obj.getC0001_007_01_T() != null) {
				for (int i = 0; i < lTipVia.size(); i++) {
					if (obj.getC0001_007_01_T().equals(lTipVia.get(i).getCOD())) {
						sp07.setSelection(i);
						break;
					}
				}
			}
			et08.setText(obj.getC0001_008_01_T());
			et09.setText(obj.getC0001_009_01_T());
			et10.setText(obj.getC0001_010_01_T());
			et11.setText(obj.getC0001_011_01_T());
			et12.setText(obj.getC0001_012_01_T());
			et13.setText(obj.getC0001_013_01_T());
			et14.setText(obj.getC0001_014_01_T());
			et15.setText(obj.getC0001_015_01_T());
			et16.setText(obj.getC0001_016_01_T());
			et17.setText(obj.getC0001_017_01_T());
			et18.setText(obj.getC0001_018_01_T());
			if (obj.getC0001_019_01_T() != null) {
				for (int i = 0; i < lDepa.size(); i++) {
					if (obj.getC0001_019_01_T().equals(lDepa.get(i).getCcdd())) {
						sp19.setSelection(i);
						ccdd = obj.getC0001_019_01_T();
						break;
					}
				}
			}
			if (obj.getC0001_022_01_T() != null) {
				for (int i = 0; i < lCiiu.size(); i++) {
					if (obj.getC0001_022_01_T().equals(lCiiu.get(i).getCIIU())) {
						sp22.setSelection(i);
						break;
					}
				}
			}
			if (obj.getC0001_024_01_T() != null) {
				for (int i = 0; i < lTipOrg.size(); i++) {
					if (obj.getC0001_024_01_T().equals(
							lTipOrg.get(i).getTIPOORG())) {
						sp24.setSelection(i);
						break;
					}
				}
			}
			et29.setText(obj.getC0001_029_01_T());
			et31.setText(obj.getC0001_031_01_T());
			et32.setText(obj.getC0001_032_01_T());
			et33.setText(obj.getC0001_033_01_T());
			et34.setText(obj.getC0001_034_01_T());
		}

		ValidarCap();
		Justificacion tmp = new Justificacion(RUC, USUARIO, "1");
		lJust = jusCon.JustificacionObtenerPorRuc(tmp);
		if (lJust == null) {
			nuevoJust = true;
			lJust = new ArrayList<Justificacion>();
			for (int i = 0; i < 22; i++) {
				Justificacion just = new Justificacion((i + 1), RUC, USUARIO,
						"1", "1");
				lJust.add(i, just);
			}
		}

		Entrevista ent = new Entrevista(RUC, USUARIO);
		lEnt = entCon.EntrevistaObtener(ent);
		if (lEnt != null) {
			Limpiar();
			for (int i = 0; i < lEnt.size(); i++) {
				View item = null;
				LayoutInflater inflater = (LayoutInflater) getActivity()
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				item = inflater.inflate(R.layout.capitulo_i_item, null);
				Entrevista e = lEnt.get(i);
				VisitasHolder holder = new VisitasHolder();
				holder.tvNro = (TextView) item.findViewById(R.id.tvNro);
				holder.tvNro.setText(e.getNRO_ENT() + "");
				holder.tv1 = (TextView) item.findViewById(R.id.tv1);
				holder.tv1.setText(e.getC0024_000_01_T());
				holder.tv2 = (TextView) item.findViewById(R.id.tv2);
				holder.tv2.setText(e.getC0024_000_02_T());
				holder.tv3 = (TextView) item.findViewById(R.id.tv3);
				holder.tv3.setText(e.getC0024_000_03_T());
				holder.tv4 = (TextView) item.findViewById(R.id.tv4);
				holder.tv4.setText(e.getC0024_000_04_T());
				holder.tv5 = (TextView) item.findViewById(R.id.tv5);
				holder.tv5.setText(e.getC0024_000_05_T());
				holder.tv6 = (TextView) item.findViewById(R.id.tv6);
				holder.tv6.setText(e.getC0024_000_06_T());

				visitas.add(item);
				llVisitas.addView(item);

			}
		}
	}

	public void Limpiar() {
		llVisitas.removeAllViews();
		visitas = new ArrayList<View>();
	}

	public void ValidarCap() {
		if (et01.getText().toString().trim().length() != 11) {
			et01.requestFocus();
			et01.setBackgroundResource(R.drawable.cap_celda_error);
		} else {
			et01.setBackgroundResource(R.drawable.cap_celda);
		}
		if (et02.getText().toString().trim().length() == 0) {
			et02.requestFocus();
			et02.setBackgroundResource(R.drawable.cap_celda_error);
		} else {
			et02.setBackgroundResource(R.drawable.cap_celda);
		}
		if (et04.getText().toString().trim().length() == 0) {
			et04.setBackgroundResource(R.drawable.cap_celda_error);
			et04.requestFocus();
		} else {
			if (et04.getText().toString().trim().length() < 6) {
				et04.setBackgroundResource(R.drawable.cap_celda_error);
				et04.requestFocus();
			} else {
				et04.setBackgroundResource(R.drawable.cap_celda);
			}
		}
		if (et05.getText().toString().trim().length() != 0
				&& et05.getText().toString().trim().length() < 6) {
			et05.setBackgroundResource(R.drawable.cap_celda_error);
			et05.requestFocus();
		} else {
			et05.setBackgroundResource(R.drawable.cap_celda);
		}
		if (et06.getText().toString().trim().length() != 0
				&& et06.getText().toString().trim().length() < 6) {
			et06.setBackgroundResource(R.drawable.cap_celda_error);
			et06.requestFocus();
		} else {
			et06.setBackgroundResource(R.drawable.cap_celda);
		}
		if (sp07.getSelectedItemPosition() == 0) {
			sp07.setBackgroundResource(R.drawable.cap_celda_error);
		} else {
			sp07.setBackgroundResource(R.drawable.cap_celda);
		}
		if (et08.getText().toString().trim().length() == 0) {
			et08.setBackgroundResource(R.drawable.cap_celda_error);
			et08.requestFocus();
		} else {
			et08.setBackgroundResource(R.drawable.cap_celda);
		}
		if (et11.getText().toString().trim().length() == 0) {
			et11.setBackgroundResource(R.drawable.cap_celda_error);
			et11.requestFocus();
		} else {
			et11.setBackgroundResource(R.drawable.cap_celda);
		}
		if (et15.getText().toString().trim().length() == 0) {
			et15.setBackgroundResource(R.drawable.cap_celda_error);
			et15.requestFocus();
		} else {
			et15.setBackgroundResource(R.drawable.cap_celda);
		}
		if (et16.getText().toString().trim().length() == 0) {
			et16.setBackgroundResource(R.drawable.cap_celda_error);
			et16.requestFocus();
		} else {
			if (Integer.parseInt(et16.getText().toString()) > 2013) {
				et16.setBackgroundResource(R.drawable.cap_celda_error);
				et16.requestFocus();
			} else {
				et16.setBackgroundResource(R.drawable.cap_celda);
			}
		}
		if (sp19.getSelectedItemPosition() == 0) {
			sp19.setBackgroundResource(R.drawable.cap_celda_error);
		} else {
			sp19.setBackgroundResource(R.drawable.cap_celda);
		}
		if (sp20.getSelectedItemPosition() == 0) {
			sp20.setBackgroundResource(R.drawable.cap_celda_error);
		} else {
			sp20.setBackgroundResource(R.drawable.cap_celda);
		}
		if (sp21.getSelectedItemPosition() == 0) {
			sp21.setBackgroundResource(R.drawable.cap_celda_error);
		} else {
			sp21.setBackgroundResource(R.drawable.cap_celda);
		}
		if (sp20.getSelectedItemPosition() == 0) {
			sp20.setBackgroundResource(R.drawable.cap_celda_error);
		} else {
			sp20.setBackgroundResource(R.drawable.cap_celda);
		}
		if (sp21.getSelectedItemPosition() == 0) {
			sp21.setBackgroundResource(R.drawable.cap_celda_error);
		} else {
			sp21.setBackgroundResource(R.drawable.cap_celda);
		}
		if (sp22.getSelectedItemPosition() == 0) {
			sp22.setBackgroundResource(R.drawable.cap_celda_error);
		} else {
			sp22.setBackgroundResource(R.drawable.cap_celda);
		}
		if (et31.getText().toString().trim().length() != 0
				&& et29.getText().toString().trim().length() == 0) {
			et29.setBackgroundResource(R.drawable.cap_celda_error);
			et29.requestFocus();
		} else {
			et29.setBackgroundResource(R.drawable.cap_celda);
		}
		if (et31.getText().toString().trim().length() != 0
				&& et32.getText().toString().trim().length() == 0) {
			et34.setBackgroundResource(R.drawable.cap_celda_error);
			et34.requestFocus();
		} else {
			et34.setBackgroundResource(R.drawable.cap_celda);
		}
		if (et29.getText().toString().trim().length() != 0
				&& et33.getText().toString().trim().length() == 0) {
			et33.setBackgroundResource(R.drawable.cap_celda_error);
			et33.requestFocus();
		} else {
			et33.setBackgroundResource(R.drawable.cap_celda);
		}
		if (et31.getText().toString().trim().length() != 0
				&& et34.getText().toString().trim().length() == 0) {
			et34.setBackgroundResource(R.drawable.cap_celda_error);
			et34.requestFocus();
		} else {
			et34.setBackgroundResource(R.drawable.cap_celda);
		}
	}

	public void GuardarCap() {
		obj.setC0001_001_01_T(et01.getText().toString());
		obj.setC0001_002_01_T(et02.getText().toString());
		obj.setC0001_003_01_T(et03.getText().toString());
		obj.setC0001_004_01_T(et04.getText().toString());
		obj.setC0001_005_01_T(et05.getText().toString());
		obj.setC0001_006_01_T(et06.getText().toString());
		obj.setC0001_007_01_T(((TipoVia) sp07.getSelectedItem()).getCOD());
		obj.setC0001_008_01_T(et08.getText().toString());
		obj.setC0001_009_01_T(et09.getText().toString());
		obj.setC0001_010_01_T(et10.getText().toString());
		obj.setC0001_011_01_T(et11.getText().toString());
		obj.setC0001_012_01_T(et12.getText().toString());
		obj.setC0001_013_01_T(et13.getText().toString());
		obj.setC0001_014_01_T(et14.getText().toString());
		obj.setC0001_015_01_T(et15.getText().toString());
		obj.setC0001_016_01_T(et16.getText().toString());
		obj.setC0001_017_01_T(et17.getText().toString());
		obj.setC0001_018_01_T(et18.getText().toString());
		obj.setC0001_019_01_T(((Dep) sp19.getSelectedItem()).getCcdd());
		obj.setC0001_020_01_T(((Pro) sp20.getSelectedItem()).getCcpp());
		obj.setC0001_021_01_T(((Dis) sp21.getSelectedItem()).getCcdi());
		obj.setC0001_022_01_T(((Ciiu) sp22.getSelectedItem()).getCIIU());
		obj.setC0001_023_01_T(((Clanae) sp23.getSelectedItem()).getCLANAE());
		obj.setC0001_024_01_T(((TipoOrganizacion) sp24.getSelectedItem())
				.getTIPOORG());
		obj.setC0001_029_01_T(et29.getText().toString());
		obj.setC0001_031_01_T(et31.getText().toString());
		obj.setC0001_032_01_T(et32.getText().toString());
		obj.setC0001_033_01_T(et33.getText().toString());
		obj.setC0001_034_01_T(et34.getText().toString());

		if (nuevo) {
			if (cap1Con.Cap01Insertar(obj) > 0) {
				nuevo = false;
				ValidarJust();
				ValidarEstado();
				m.Exito("SE GRABO CON EXITO");

			} else {
				m.Error("ERROR AL GRABAR");
			}
		} else {
			if (cap1Con.Cap01Modificar(obj) > 0) {
				ValidarJust();
				ValidarEstado();
				m.Exito("SE MODIFICO CON EXITO");
			} else {
				m.Error("ERROR AL MODIFICAR");
			}
		}
	}

	private void ValidarJust() {
		if (et01.getText().toString().trim().length() != 11) {
			lJust.get(0).setFLAG("0");
		} else {
			lJust.get(0).setFLAG("1");
		}
		if (et02.getText().toString().trim().length() == 0) {
			lJust.get(1).setFLAG("0");
		} else {
			lJust.get(1).setFLAG("1");
		}
		if (et04.getText().toString().trim().length() == 0) {
			lJust.get(2).setFLAG("0");
		} else {
			lJust.get(2).setFLAG("1");
		}
		if (et04.getText().toString().trim().length() < 6) {
			lJust.get(3).setFLAG("0");
		} else {
			lJust.get(3).setFLAG("1");
		}
		if (et05.getText().toString().trim().length() != 0
				&& et05.getText().toString().trim().length() < 6) {
			lJust.get(4).setFLAG("0");
		} else {
			lJust.get(4).setFLAG("1");
		}
		if (et06.getText().toString().trim().length() != 0
				&& et06.getText().toString().trim().length() < 6) {
			lJust.get(5).setFLAG("0");
		} else {
			lJust.get(5).setFLAG("1");
		}
		if (sp07.getSelectedItemPosition() == 0) {
			lJust.get(6).setFLAG("0");
		} else {
			lJust.get(6).setFLAG("1");
		}
		if (et08.getText().toString().trim().length() == 0) {
			lJust.get(7).setFLAG("0");
		} else {
			lJust.get(7).setFLAG("1");
		}
		if (et11.getText().toString().trim().length() == 0) {
			lJust.get(8).setFLAG("0");
		} else {
			lJust.get(8).setFLAG("1");
		}
		if (et15.getText().toString().trim().length() == 0) {
			lJust.get(9).setFLAG("0");
		} else {
			lJust.get(9).setFLAG("1");
		}
		if (et16.getText().toString().trim().length() == 0) {
			lJust.get(10).setFLAG("0");
		} else {
			lJust.get(10).setFLAG("1");
		}
		if (Integer
				.parseInt(et16.getText().toString().trim().length() == 0 ? "0"
						: et16.getText().toString()) > 2013) {
			lJust.get(11).setFLAG("0");
		} else {
			lJust.get(11).setFLAG("1");
		}
		if (sp19.getSelectedItemPosition() == 0) {
			lJust.get(12).setFLAG("0");
		} else {
			lJust.get(12).setFLAG("1");
		}
		if (sp20.getSelectedItemPosition() == 0) {
			lJust.get(13).setFLAG("0");
		} else {
			lJust.get(13).setFLAG("1");
		}
		if (sp21.getSelectedItemPosition() == 0) {
			lJust.get(14).setFLAG("0");
		} else {
			lJust.get(14).setFLAG("1");
		}
		if (sp20.getSelectedItemPosition() == 0) {
			lJust.get(15).setFLAG("0");
		} else {
			lJust.get(15).setFLAG("1");
		}
		if (sp21.getSelectedItemPosition() == 0) {
			lJust.get(16).setFLAG("0");
		} else {
			lJust.get(16).setFLAG("1");
		}
		if (sp22.getSelectedItemPosition() == 0) {
			lJust.get(17).setFLAG("0");
		} else {
			lJust.get(17).setFLAG("1");
		}
		if (et31.getText().toString().trim().length() != 0
				&& et29.getText().toString().trim().length() == 0) {
			lJust.get(18).setFLAG("0");
		} else {
			lJust.get(18).setFLAG("1");
		}
		if (et31.getText().toString().trim().length() != 0
				&& et32.getText().toString().trim().length() == 0) {
			lJust.get(19).setFLAG("0");
		} else {
			lJust.get(19).setFLAG("1");
		}
		if (et31.getText().toString().trim().length() != 0
				&& et33.getText().toString().trim().length() == 0) {
			lJust.get(20).setFLAG("0");
		} else {
			lJust.get(20).setFLAG("1");
		}
		if (et31.getText().toString().trim().length() != 0
				&& et34.getText().toString().trim().length() == 0) {
			lJust.get(21).setFLAG("0");
		} else {
			lJust.get(21).setFLAG("1");
		}

		if (nuevoJust) {
			for (int i = 0; i < 22; i++) {
				jusCon.JustificacionInsertar(lJust.get(i));
			}
			nuevoJust = false;
		} else {
			for (int i = 0; i < 22; i++) {
				jusCon.JustificacionModificar(lJust.get(i));
			}
		}
	}

	private void ValidarEstado() {
		MarcoController marCon = new MarcoController(getActivity());
		Marco mar = new Marco(RUC, USUARIO);
		mar = marCon.MarcoObtener(mar);
		mar.setVALIDADO("2");
		Justificacion jus1 = new Justificacion(RUC, USUARIO, "1");
		List<Justificacion> lJus1 = jusCon.JustificacionObtenerPorRuc(jus1);
		Justificacion jus2 = new Justificacion(RUC, USUARIO, "2");
		List<Justificacion> lJus2 = jusCon.JustificacionObtenerPorRuc(jus2);
		Justificacion jus3 = new Justificacion(RUC, USUARIO, "3");
		List<Justificacion> lJus3 = jusCon.JustificacionObtenerPorRuc(jus3);
		int continuar = 1;

		if (lJus1 != null && continuar == 1) {
			for (int i = 0; i < lJus1.size(); i++) {
				if (lJus1.get(i).getFLAG().equals("0")) {
					mar.setVALIDADO("1");
					continuar = 0;
					break;
				}
			}
		}
		if (lJus2 != null && continuar == 1) {
			for (int i = 0; i < lJus2.size(); i++) {
				if (lJus2.get(i).getFLAG().equals("0")) {
					mar.setVALIDADO("1");
					continuar = 0;
					break;
				}
			}
		}
		if (lJus3 != null && continuar == 1) {
			for (int i = 0; i < lJus3.size(); i++) {
				if (lJus3.get(i).getFLAG().equals("0")) {
					mar.setVALIDADO("1");
					continuar = 0;
					break;
				}
			}
		}

		marCon.MarcoActualizar(mar);
	}
}
